package cn.quickcode.monitor.ui.part;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import cn.quickcode.monitor.SMR;
import cn.quickcode.monitor.core.Task;
import cn.quickcode.monitor.core.TaskManager;
import cn.quickcode.monitor.model.Ntm;
import cn.quickcode.monitor.ui.dialog.NewNTMDialog;
import cn.quickcode.monitor.ui.provider.SMContentProvider;
import cn.quickcode.monitor.ui.provider.SMLabelProvider;

/**
 * 网络监控
 * 
 * @author zhangaoxiang
 * @version $Revision$ - $Author$ at $Date$
 */
public class NTMViewPart extends ViewPart
{

	public static final String ID = "cn.quickcode.monitor.ui.part.NTMViewPart"; //$NON-NLS-1$

	public Text text;

	private TreeViewer treeViewer;

	private Tree tree;
	
	public Action cfgAction;

	public Action newAction;
	
	public Action runAction;

	public NTMViewPart()
	{
	}

	/**
	 * Create contents of the view part.
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent)
	{
		Composite container = new Composite(parent, SWT.NONE);
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.verticalSpacing = 2;
		gl_container.marginWidth = 2;
		gl_container.marginHeight = 2;
		container.setLayout(gl_container);
		this.text = new Text(container, SWT.BORDER);
		this.text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		this.text.addKeyListener(new KeyAdapter()
		{
			public void keyReleased(KeyEvent ke)
			{
				NTMViewPart.this.refresh(text.getText());
			}
		});
		this.treeViewer = new TreeViewer(container, SWT.BORDER|SWT.FULL_SELECTION);
		this.tree = treeViewer.getTree();
		this.tree.setLinesVisible(true);
		this.tree.setHeaderVisible(true);
		this.tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		this.createTreeViewerColumns();

		createActions();
		initializeToolBar();
		this.refresh(null);
	}

	private void createTreeViewerColumns()
	{
		{
			TreeViewerColumn treeViewerColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
			TreeColumn trclmnNewColumn = treeViewerColumn.getColumn();
			trclmnNewColumn.setWidth(38);
			trclmnNewColumn.setText("序号");
		}
		{
			TreeViewerColumn treeViewerColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
			TreeColumn trclmnNewColumn_1 = treeViewerColumn.getColumn();
			trclmnNewColumn_1.setWidth(100);
			trclmnNewColumn_1.setText("名称");
		}
		{
			TreeViewerColumn treeViewerColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
			TreeColumn trclmnNewColumn_2 = treeViewerColumn.getColumn();
			trclmnNewColumn_2.setWidth(100);
			trclmnNewColumn_2.setText("地址");
		}
		{
			TreeViewerColumn treeViewerColumn = new TreeViewerColumn(this.treeViewer, SWT.NONE);
			TreeColumn trclmnNewColumn_1 = treeViewerColumn.getColumn();
			trclmnNewColumn_1.setWidth(38);
			trclmnNewColumn_1.setText("状态");
		}
	}

	/**
	 * Create the actions.
	 */
	private void createActions()
	{
		{
			this.newAction = new Action()
			{
				public void run()
				{
					NewNTMDialog dialog = new NewNTMDialog(getViewSite().getShell(), NTMViewPart.this);
					dialog.open();
				}
			};
			this.newAction.setText("添加");
			this.newAction.setToolTipText("添加新的监控项");
			this.newAction.setImageDescriptor(SMR.BUTTON_ACTION_NEW);
		}
		{
			this.cfgAction = new Action()
			{

				public void run()
				{
					MessageDialog.openInformation(getSite().getShell(), "配置", "配置");
				}
			};
			this.cfgAction.setText("配置");
			this.cfgAction.setToolTipText("监控参数配置");
			this.cfgAction.setImageDescriptor(SMR.BUTTON_ACTION_CFG);
		}
		{
			this.runAction = new Action()
			{

				public void run()
				{
					if(NTMViewPart.this.runAction.getImageDescriptor().equals(SMR.BUTTON_ACTION_STR))
					{
						if(!TaskManager.exists(Ntm.TASK_ID))
						{
							NTMViewPart ntmpart = (NTMViewPart)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(NTMViewPart.ID);
							Task<Ntm> task = new Task<Ntm>();
							task.setTreeViewer(ntmpart.treeViewer);
							task.setId(Ntm.TASK_ID);
							task.setName("网络监控");
							task.setDelay(5000);
							TaskManager.addTask(task);
						}
						TaskManager.opentask(Ntm.TASK_ID);
						NTMViewPart.this.runAction.setImageDescriptor(SMR.BUTTON_ACTION_STP);
						NTMViewPart.this.runAction.setText("停止");
						NTMViewPart.this.runAction.setToolTipText("停止监控");
					}
					else
					{
						TaskManager.shuttask(Ntm.TASK_ID);
						NTMViewPart.this.runAction.setImageDescriptor(SMR.BUTTON_ACTION_STR);
						NTMViewPart.this.runAction.setText("开启");
						NTMViewPart.this.runAction.setToolTipText("开启监控");
					}
				}
			};
			this.runAction.setImageDescriptor(SMR.BUTTON_ACTION_STR);
			this.runAction.setText("开启");
			this.runAction.setToolTipText("开启监控");
		}
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar()
	{
		IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
		toolbarManager.add(this.runAction);
		toolbarManager.add(this.newAction);
		toolbarManager.add(this.cfgAction);
	}
	@Override
	public void setFocus()
	{
		// Set the focus
	}
	public void refresh(String name)
	{
		this.treeViewer.setContentProvider(new SMContentProvider());
		this.treeViewer.setLabelProvider(new SMLabelProvider());
		this.treeViewer.setInput(Ntm.query(name));
	}
}
